package br.com.dyad.commons.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import br.com.dyad.commons.zip.ZipUtils;


public class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;

    public DesEncrypter(String stringKey) {         
    	// Create an 8-byte initialization vector
        byte[] iv = {(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
                	 (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03};
        int iterationCount = 19;
        
        //AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(iv, iterationCount);
        try {
        	KeySpec keySpec = new PBEKeySpec(stringKey.toCharArray(), iv, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
            
        	ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // CBC requires an initialization vector
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }
    }

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[1024];

    public void encrypt(String in, String out) {
        try {
        	FileInputStream fileInputStream = new FileInputStream(in);
        	FileOutputStream fileOutputStream = new FileOutputStream(out);
        	try{        		
        		encrypt(fileInputStream, fileOutputStream);
        	} finally {
        		fileInputStream.close();
        		fileOutputStream.close();
        	}
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }
    }
    
    public void encrypt(InputStream in, OutputStream out) {
        try {
            // Bytes written to out will be encrypted
            out = new CipherOutputStream(out, ecipher);

            // Read in the cleartext bytes and write to out to encrypt
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public void decrypt(String in, String out) {
    	try {
        	FileInputStream fileInputStream = new FileInputStream(in);
        	FileOutputStream fileOutputStream = new FileOutputStream(out);
        	try{        		
        		decrypt(fileInputStream, fileOutputStream);
        	} finally {
        		fileInputStream.close();
        		fileOutputStream.close();
        	}
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }
    }
    
    public void decrypt(InputStream in, OutputStream out) {
        try {
            // Bytes read from in will be decrypted
            in = new CipherInputStream(in, dcipher);

            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        	throw new RuntimeException(e);
        }
    }
}